package poder.jsf2.beans;



import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

//import org.apache.log4j.Logger;



@ManagedBean(name="loginBean")
@SessionScoped
public class Seguridad {
	private final Logger log = Logger.getLogger(Seguridad.class);
	private static ResourceBundle rb = ResourceBundle.getBundle("mensaje01.messages");
	
    private static final String[] users = {"anna:qazwsx","kate:123456"};
	
    @ManagedProperty(value="#{navigationBean}")
    private NavigationBean navigationBean;
    
    public String userId;
    public String password;
    public Boolean loggedIn;
    
    
	public NavigationBean getNavigationBean() {
		log.debug("getNavigationBean: "+navigationBean);
		return navigationBean;
	}
	public void setNavigationBean(NavigationBean navigationBean) {
		log.debug("setNavigationBean: "+navigationBean);
		this.navigationBean = navigationBean;
	}
	public String getUserId() {
		log.debug("getUserId: "+userId);
		return userId;
	}
	public void setUserId(String userId) {
		log.debug("setUserId: "+userId);
		this.userId = userId;
	}
	public String getPassword() {
		log.debug("getPassword: "+password);
		return password;
	}
	public void setPassword(String password) {
		log.debug("setPassword: "+password);
		this.password = password;
	}
	
	public Boolean getLoggedIn() {
		log.debug("getLoggedIn: "+loggedIn);
		return loggedIn;
	}
	
	public void setLoggedIn(Boolean loggedIn) {
		log.debug("setLoggedIn: "+loggedIn);
		this.loggedIn = loggedIn;
	}
	public static String[] getUsers() {
		return users;
	}
	
	public String doLogin() {

		log.debug("doLogin");
		String error_user_or_pass=null;
		
        for (String user: users) {
            String dbUsername = user.split(":")[0];
            String dbPassword = user.split(":")[1];
             
            
            if (dbUsername.equals(userId) && dbPassword.equals(password)) {
            	//rb.getString();
                loggedIn = true;
                return navigationBean.redirectToWelcome();
            }
        }
         
        
        // Set login ERROR
        error_user_or_pass = rb.getString("message.error_user_or_pass");
        FacesMessage msg = new FacesMessage(error_user_or_pass, "ERROR MSG");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, msg);
         
        // To to login page
        return navigationBean.toLogin();
         
    }
     
    /**
     * Logout operation.
     * @return
     */
    public String doLogout() {
		 //Set the paremeter indicating that user is logged in to false
		 
		 log.debug("doLogout");
		 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
		 //HttpSession session = (HttpSession) FacesContext.getCurrentInstance();
		 loggedIn = false;
		 // Set logout message
		FacesMessage msg = new FacesMessage("Logout success!", "INFO MSG");
		msg.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, msg);
        
		return navigationBean.toLogin();
    }

}
